<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table>
        <tr>
            <td>标题:</td>
            <td><input type="text" name="title"></td>
        </tr>
        <tr>
            <td>摘要:</td>
            <td><input type="text" name="zhaoyao"></td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><input type="text" name="neirong"></td>
        </tr>
        <tr>
            <td>分类:</td>
            <td><input type="text" name="fenlei"></td>
        </tr>
        <tr>
            <td>作者:</td>
            <td><input type="text" name="zuozhe"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="返回" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config//baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script>
        $(function () {
            let url = location.search
            if (url) {
                let id = url.split('?')[1].split('=')[1]
                getListById(id).then(res => {
                    $('[name=title]').val(res.data.title)
                    $('[name=zhaoyao]').val(res.data.zhaoyao)
                    $('[name=neirong]').val(res.data.neirong)
                    $('[name=fenlei]').val(res.data.fenlei)
                    $('[name=zuozhe]').val(res.data.zuozhe)
                })
            } else {

            }
        })
        function save() {
            if (confirm('确定保存?')) {
                let url = location.search
                let obj = {
                    title: $('[name=title]').val(),
                    zhaoyao: $('[name=zhaoyao]').val(),
                    neirong: $('[name=neirong]').val(),
                    fenlei: $('[name=fenlei]').val(),
                    zuozhe: $('[name=zuozhe]').val(),
                }
                console.log(obj);
                if (url) {
                    let id = url.split('?')[1].split('=')[1]
                    console.log(id);
                    updateBlog(id, obj).then(res => {
                        location.href = 'index.html'
                    })
                } else {
                    addBlog(obj).then(res => {
                        location.href = 'index.html'
                    })
                }
            }

        }
        function cancel() {
            location.href = 'index.html'
        }
    </script>
</body>

</html>